package ui2.player
{
   import flash.display.MovieClip;
   import flash.display.BlendMode;
   import flash.display.StageDisplayState;
   import flash.events.Event;
   import flash.events.MouseEvent;
   import flash.events.FullScreenEvent;
   import flash.geom.Point;
   import flash.utils.setTimeout;
   import flash.utils.clearTimeout;
   // greensock
   import com.greensock.TweenMax;
   import com.greensock.easing.Quart;
   // boy
   import ui2.MyPlayerMgr;

   /**
    * @author  boy | cjboy1984@gmail.com
    */
   public class ControllerPanel_1 extends MovieClip
   {
      // org pos, for fullscreen
      private var orgXY:Point;
      private var _fullscreen:Boolean;

      // timeout id
      private var tid:uint;

      // mouse-over flag
      private var isMouseOver:Boolean = false;

      public function ControllerPanel_1()
      {
         // disable tab-functionality.
         tabEnabled = false;
         tabChildren = false;
         focusRect = false;

         mouseChildren = true;

         stop();
         addEventListener(Event.ADDED_TO_STAGE, _onAdd);
         addEventListener(Event.REMOVED_FROM_STAGE, _onRemove);
      }

      // ################# Protected Func ###################

      // ################### Private Func ###################

      private function _onAdd(e:Event = null):void
      {
         // save org pos
         orgXY = new Point(x, y);

         mgr.addEventListener(MyPlayerMgr.BUFFERING, onBuffering);
         mgr.addEventListener(MyPlayerMgr.MOUSE_MOVE, onShowThis);
         mgr.addEventListener(MyPlayerMgr.ROLL_OVER, onShowThis);
         mgr.addEventListener(MyPlayerMgr.ROLL_OUT, onHideThis);

         // this
         addEventListener(MouseEvent.ROLL_OVER, onThisOver);
         addEventListener(MouseEvent.ROLL_OUT, onThisOut);

         // stage full screen
         stage.addEventListener(FullScreenEvent.FULL_SCREEN, onEnterFullscreen);
         stage.addEventListener(Event.RESIZE, onStageResize);
      }

      private function _onRemove(e:Event = null):void
      {
         // tween
         TweenMax.killTweensOf(this);

         // clear timeout event
         clearTimeout(tid);

         mgr.removeEventListener(MyPlayerMgr.BUFFERING, onBuffering);
         mgr.removeEventListener(MyPlayerMgr.MOUSE_MOVE, onShowThis);
         mgr.removeEventListener(MyPlayerMgr.ROLL_OVER, onShowThis);
         mgr.removeEventListener(MyPlayerMgr.ROLL_OUT, onHideThis);

         // this
         removeEventListener(MouseEvent.ROLL_OVER, onThisOver);
         removeEventListener(MouseEvent.ROLL_OUT, onThisOut);

         // stage full screen
         stage.removeEventListener(FullScreenEvent.FULL_SCREEN, onEnterFullscreen);
         stage.removeEventListener(Event.RESIZE, onStageResize);
      }

      // -------------------- LINE -------------------

      private function onThisOver(e:MouseEvent):void { isMouseOver = true; }
      private function onThisOut(e:MouseEvent):void { isMouseOver = false; }

      // -------------------- LINE -------------------

      private function onBuffering(e:Event):void
      {
         if (mgr.loadPerc < 100)
         {
            onShowThis();
         }
         else
         {
            onHideThis();
         }
      }

      private function onShowThis(e:Event = null):void
      {
         // clear timeout
         clearTimeout(tid);
         visible = true;
         TweenMax.to(this, 0.2, { alpha:1, ease:Quart.easeOut, onComplete:delayHide } );
      }

      private function onHideThis(e:Event = null):void
      {
         if (!isMouseOver)
         {
            TweenMax.to(this, 0.2, { alpha:0, visible:false, ease:Quart.easeIn } );
         }
      }

      // -------------------- LINE -------------------

      private function delayHide():void
      {
         if (!isMouseOver)
         {
            tid = setTimeout(onHideThis, 1 * 1000);
         }
      }

      // -------------------- LINE -------------------

      // 先觸發fullscreen事件才觸發resize事件
      private function onEnterFullscreen(e:FullScreenEvent):void
      {
         _fullscreen = e.fullScreen;
      }

      private function onStageResize(e:Event):void
      {
         if (_fullscreen)
         {
            var locPos:Point = parent.globalToLocal(new Point(sw / 2, sh));
            x = locPos.x;
            y = locPos.y;
         }
         else
         {
            x = orgXY.x;
            y = orgXY.y;
         }

         // hide it
         isMouseOver = false;
         onHideThis();
      }

      // -------------------- LINE -------------------

      private function get mgr():MyPlayerMgr { return MyPlayerMgr.getMgrAt(0); }

      // -------------------- LINE -------------------

      private function get sw():Number { return stage.stageWidth; }
      private function get sh():Number { return stage.stageHeight; }

      // -------------------- LINE -------------------

   }

}